﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Base;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Entities;
using KaytechSoftware.Cms.App.Areas.Admin.Models.Filters;
using KaytechSoftware.Cms.App.Common;
using KaytechSoftware.Cms.Services.Dto;
using KaytechSoftware.Cms.Services.Interfaces;
using System.Linq;
using KaytechSoftware.Cms.Services.Interfaces.Entity;

namespace KaytechSoftware.Cms.App.Areas.Admin.Controllers
{
    public class PageController : GridController<PageFiltersModel, PageModel, PageDto>
    {
        private readonly IPageService _pageService;
        private readonly ILeftSideRendererService _leftSideRendererService;
        private readonly IRightSideRendererService _rightSideRendererService;

        public PageController(IPageService pageService, ILeftSideRendererService leftSideRendererService, IRightSideRendererService rightSideRendererService) : base(pageService)
        {
            _pageService = pageService;
            _leftSideRendererService = leftSideRendererService;
            _rightSideRendererService = rightSideRendererService;
        }

        public override ActionResult Add()
        {
            var leftRendererDtos = _leftSideRendererService.GetAll();
            var rightRendererDtos = _rightSideRendererService.GetAll();

            var model = CreateEntityModel();
            model.RegisterCollection(CollectionKey.LeftSideRenderer, leftRendererDtos, l => l.Id, l => l.Code);
            model.RegisterCollection(CollectionKey.RightSideRenderer, rightRendererDtos, l => l.Id, l => l.Code);

            return Details(model);
        }

        public override ActionResult Edit(long id)
        {
            var pageDto = _pageService.GetDtoById(id);
            var leftRendererDtos = _leftSideRendererService.GetAll();
            var rightRendererDtos = _rightSideRendererService.GetAll();
            
            var model = CreateEntityModel();
            model.FromDto(pageDto);
            model.RegisterCollection(CollectionKey.LeftSideRenderer, leftRendererDtos, l => l.Id, l => l.Code);
            model.RegisterCollection(CollectionKey.RightSideRenderer, rightRendererDtos, l => l.Id, l => l.Code);

            return Details(model);
        }

        public override void OnPreSave(PageModel editModel)
        {
        }

        public override PageFiltersModel RestoreOrGetDefaultFilters()
        {
            return new PageFiltersModel();
        }

        public override void RegisterCollectionsForFilters(PageFiltersModel filters)
        {

        }

        protected override IList<PageDto> Filter(PageFiltersModel filtersModel)
        {
            var items = _pageService.GetAll();
            return items;
        }
    }
}
